package com.yu.util;

import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

public class GeneratorNumber {
	private static int MINUTES = 0;

	private static int SECONDS = 0;

	public static String get12Id() {
		Calendar c = Calendar.getInstance();
		String strDay, strSeconds;
		strDay = TimeUtil.getCurrMinDate();

		// minutes
		int minutes = c.get(Calendar.MINUTE);
		if (minutes != MINUTES) {
			MINUTES = minutes;
			SECONDS = 0;
		}
		// seconds
		if (SECONDS < 10) {
			strSeconds = "0" + SECONDS;
		} else {
			strSeconds = "" + SECONDS;
		}
		if (SECONDS > 99) {
			return null;
		}

		SECONDS++;

		return strDay + strSeconds;
	}

	/**
	 * 典型的随机洗牌算法。 流程是从备选数组中选择一个放入目标数组中，将选取的数组从备选数组移除（放至最后，并缩小选择区域） 算法时间复杂度O(n)
	 * 
	 * @return 随机8为不重复数组
	 */
	public static String get6Id() {
		// 初始化备选数组
		int[] defaultNums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int[] nums = new int[6];

		Random random = new Random();
		StringBuffer generateNumber = new StringBuffer();

		// 默认数组中可以选择的部分长度
		int canBeUsed = 10;
		// 填充目标数组
		for (int i = 0; i < nums.length; i++) {
			// 将随机选取的数字存入目标数组
			int index = random.nextInt(canBeUsed);
			nums[i] = defaultNums[index];
			generateNumber.append(nums[i]);
			// 将已用过的数字扔到备选数组最后，并减小可选区域
			swap(index, canBeUsed - 1, defaultNums);
			canBeUsed--;
		}

		return generateNumber.toString();
	}

	private static void swap(int i, int j, int[] nums) {
		int temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}

	public static String get32Id() {
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 20; i++)
			System.out.println(get6Id());
	}
}